package nju.firework.res;

import java.util.HashMap;
import java.util.Map;

import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;

import nju.firework.game.main.MainActivity;

public class ResourceManager {
	/**
	 * 
	 */
	MainActivity mGame;
	TextureFactory mTextureFactory;
	
	BitmapTextureAtlas heroRes;
	BitmapTextureAtlas cellRes;
	
	
	static Map<String,ITextureRegion> resMap;
	
	public ResourceManager(MainActivity pGame){
		this.mGame = pGame;
		mTextureFactory = new TextureFactory(pGame);
		resMap = new HashMap<String, ITextureRegion>();
		initTextureRes();
	}
	
	public void setBase(final String path){
		TextureFactory.setAssetBasePath(path);
	}
	
	public void loadTexture(String path, ResType type, String name, int x, int y){
		TextureRegion tmp = mTextureFactory.create(toRes(type), path, x, y);
		resMap.put(name, tmp);
	}
	
	public void loadTiledTexture(String path, ResType type, String name, int x, int y, int columns, int rows){
		TiledTextureRegion tmp = mTextureFactory.createTile(toRes(type), path, x, y, columns, rows);
		resMap.put(name,tmp);
	}
	
	public static ITextureRegion getTexture(String textureName){
		return (ITextureRegion) resMap.get(textureName);
	}
	
	public void endLoad(){
		heroRes.load();
		cellRes.load();
	}
	
	private void initTextureRes(){
		heroRes = new BitmapTextureAtlas(mGame.getTextureManager(), 
				ResConst.heroResWidth, ResConst.heroResHeight, TextureOptions.BILINEAR);
		cellRes = new BitmapTextureAtlas(mGame.getTextureManager(),
				ResConst.cellResWidh, ResConst.heroResHeight, TextureOptions.BILINEAR);
	}
	
	private BitmapTextureAtlas toRes(ResType type){
		switch(type){
		case HERO : return heroRes;
		case CELL : return cellRes;
		default : return null;
		}
	}
}
